import easygui as g
import turtle
import sys,os
from PIL import Image, ImageDraw, ImageFont

msg = ''
title = '编程猫用户登录'
uinfo = []#列表
uinfo = g.multpasswordbox(msg,title,('用户名','密码'))
#print(uinfo)
u = 'edu123'
p = '123456'

#重启程序
def restart():
    python = sys.executable
    os.execl(python,python,* sys.argv)

#表情包生成器
def emo():
    msg = "请填写信息"
    title = "表情包生成器"
    fieldName = ["原图片（含后缀）", "另存为（含后缀）", "添加文字", "文字大小", "文字位置（X轴）", "文字位置（Y轴）"]
    fieldValues = g.multenterbox(msg, title, fieldName)
    if fieldValues == None:
        login_ok()
    imgIn = fieldValues[0]
    imgOut = fieldValues[1]
    words = fieldValues[2]
    wordSize = int(fieldValues[3])
    wordX = int(fieldValues[4])
    wordY = int(fieldValues[5])
    # 加载图片
    im = Image.open(imgIn)
    draw = ImageDraw.Draw(im)
    # 设置文字的字体和大小
    font = ImageFont.truetype("simhei.ttf", wordSize)
    # 图片插入文字
    draw.text((wordX, wordY), words, fill="black", font=font)
    # 显示图片
    im.show()
    # 保存图片
    im.save(imgOut)

#海龟绘图
def turtles():
    #分形雪花
    t = turtle.Pen()
    def snow(size, n):
        if (n == 0):
            t.forward(size)
        else:
            for angel in [0, 60, (-120), 60]:
                t.left(angel)
                #函数调用自身，这种调用方式叫做递归
                #递归时参数变小，画的是更小的雪花
                snow(size / 3, n - 1)
    def draw():
        t.speed(0)
        t.penup()
        t.goto((-200), 100)
        t.pendown()
        t.pensize(2)
        level = 3
        snow(400, level)
        t.right(120)
        snow(400, level)
        t.right(120)
        snow(400, level)
        turtle.done()
    #开始进入Python的世界
    draw()

#海龟绘图界面
def t():
    b = g.buttonbox(msg='', title='编程猫', choices=('雪花', '海龟','返回'))
    if b == '雪花':
        turtles()
        t()
    elif b == '海龟绘图':
        pass
    elif b == '返回':
        login_ok()

#登录成功界面
def login_ok():
    b = g.buttonbox(msg='',title='编程猫',choices=('表情包生成器','海龟绘图'))
    if b == '表情包生成器':
        emo()
    elif b == '海龟绘图':
        t()

#登录
def login():
    if uinfo[0] == u and uinfo[1] == p:
        login_ok()
    else:
        t = g.msgbox(msg='用户名或者密码错误',title='waring')
        if t == 'OK':
            restart()
login()